<?php

/*
 * 
 * 
 * 
 *      Here is how succession works:
 * 
 *      A family is ruled by either a Patron or a Matron.
 *      This is determined by family name.  If the man left his family for marriage,
 *      then it is Matrilineal and the woman rules.  If the woman left her family
 *      for marriage, then it is patrilineal.
 *      However, this does not really influence further succession.
 *      When the head of the families dies, whether Patri or Matri, the oldest
 *      living child /of the family/ becomes the head.
 *      Lets look at the example of the Hannover Family.
 *      Matrilineal, ruled by Edessa Hannover and her consort Franklin Pierce-Hannover,
 *      When Edessa dies, her eldest child bearing the Hannover name becomes head.
 *      So if her eldest has married out of the family, he or she is ineligable for
 *      marriage.
 * 
 *      If the family head dies with no eligable children, it goes to the closest
 *      eligable relative.
 * 
 *      Divorce?  Can a widow/widower go back to their own family?
 * 
 * 
 */


// check user login
session_start();
if (isset($_SESSION["user"]) && !empty($_SESSION["user"])) {
//echo $_SESSION["user"];




class Family extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
	}
        
        public function index() {
            
            $this->load->view('toolbar_view');
            
            //$this->load->view('family/family_main_view');  // currently not in use.
            
            $this->load->model('display_family_model');
            $my_data['data'] = $this->display_family_model->show_unmarried_family();
                
            $this->load->view('family/family_display_unmarried', $my_data);
            
            $this->display_proposals_from_me();
            
        }   ////// end of function index //////////////////////
        ////////////////////////////////////////////////
        
        public function display_my_family() {
            
            $_SESSION["active_character"];
            
            // get my family
            
            // display my family
            
        }   ////// end of function display_my_family //////////////////////
        ////////////////////////////////////////////////
        
        
        
        
        public function find_spouse() {
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
    ////// this is an OLD FUNCTION
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
            
            $current_turn = '1';
            
            if ($this->_form_validate() === FALSE) {
                $this->index();
                return;    
            }
            
            //char1id
            $char1 = $this->input->post('char1id',TRUE);
            $_SESSION["wedding_char1"] = $char1;
            
            // need to get char1's age, for the view to determine what turn they can get married on
            $character1 = $this->doctrine->em->find('ORM\Dynasties2\Characters', $char1);
            //echo $user->getfirst_Name().' '.$user->getlastname().' is a real chill guy.<br />';
            $char1_born_turn = $character1 -> getBornDate();
            $_SESSION["char1_born_turn"] = $char1_born_turn;
            $char1_age_turns = abs($current_turn - $char1_born_turn);
            
            
            $this->load->model('display_family_model');
            
            
            $my_data['data'] = $this->display_family_model->show_potential_spouses();
            //$my_data['data2'] = 'elmo';    /// this works!!!!
                       
            $how_many_returned = '0';
            foreach ($my_data['data'] as $key => $row) {
                $how_many_returned++;
                echo form_open('family/propose_union');
                //echo ($row->getName()) . ($row->getId());
                //echo $row -> getName();
                $born_turn = $row -> getBornDate();
                //echo $born_turn;
                $char2_age_turns = abs($current_turn - $born_turn);
                
                
                // are either younger than 16?
                if ($char1_age_turns < 128 OR $char2_age_turns < 128) {
                    // one is not of age
                        $age_diff = $char1_age_turns  - $char2_age_turns;
                        // if $age_diff is a positive number, then char1 is older
                        // if its negative, char2 is older
                        // if its neutral then neither is older

                        if ($age_diff > '0') {
                            //char1 older, we'll base the turn counter on char2 to determine the earliest marry turn
                            $age_turns = $char2_age_turns;
                        } else if ($age_diff < '0') {
                            // char2 older, we'll base turn counter on char to determine the earliest marry turn
                            $age_turns = $char1_age_turns;
                        } else {
                            // same age
                            $age_turns = $char2_age_turns;
                        }
                        $remaining_turns = 128 - $age_turns;
                        $first_avail = $remaining_turns + $current_turn;
                        //echo "</br>TEST TEST " . $first_avail . " TEST TEST</br>";
                        $turn_counter = $first_avail;
                        $i = '0';
                        while ($i < 8) {
                            $my_data['data2'][$how_many_returned][$turn_counter] = $turn_counter;
                            //echo $turn_counter . ", ";
                            //Message: Object of class ORM\Dynasties2\Characters could not be converted to string  wtf??

                            $i++;
                            $turn_counter++;
                        } // end while to insert values into array
                    
                    ////////// end of code for when either or both are not of age yet
                } else {
                        // both are of age
                        $i = '0';
                        $turn_counter = $current_turn;
                        $turn_counter++;
                        while ($i < 8) {
                            $my_data['data2'][$how_many_returned][$turn_counter] = $turn_counter;

                            $i++;
                            $turn_counter++;
                        } // end while to insert values into array
                    
                }
                
                
            } // end foreach checking ages
            
            
            
            
                
            $this->load->view('family/display_potential_spouses', $my_data);
            
            
        } ////////////////// end of function find_spouse
        
        public function propose_union() {
            ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
    ////// this is an OLD FUNCTION
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
            
            if ($this->_form_validate() === FALSE) {
                $this->index();
                return;    
            }
            
            $target_turn = $this->input->post('target_turn',TRUE);
            $char2id = $this->input->post('char2id',TRUE);
            $char2fam = $this->input->post('char2fam',TRUE);
            $_SESSION["wedding_char2"] = $char2id;
            $_SESSION["wedding_fam2"] = $char2fam;
            $_SESSION["target_turn"] = $target_turn;
            //char2fam
            
            $this->load->model('display_family_model');
            $this->display_family_model->send_union_proposal();
            $this->index();
            
        } /// end of function propose_union
        
        
        public function display_proposals_from_me() {
            
            ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
    ////// this is an OLD FUNCTION
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
            $this->load->model('display_family_model');
            $my_data['data'] = $this->display_family_model->load_proposals_from_me();
            $this->load->view('family/display_proposals_fromme_view', $my_data);
            
        }   /// end of function display_proposals_from_me
        
        public function display_proposals_to_me() {
            
            ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
    ////// this is an OLD FUNCTION
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
            $this->load->model('display_family_model');
            $my_data['data'] = $this->display_family_model->load_proposals_to_me();
            $this->load->view('family/display_proposals_tome_view', $my_data);
            
        }   // end of function display_proposals_to_me
         
        
        
        
        private function _form_validate() {
            
            ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
    ////// this is an OLD FUNCTION
    ////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////
            $this->form_validation->set_rules('char1id', 'character 1 id',
                    'min_length[1]|max_length[6]|trim|numeric|xss_clean');
            $this->form_validation->set_rules('char2id', 'character 2 id',
                    'min_length[1]|max_length[6]|trim|numeric|xss_clean');
            $this->form_validation->set_rules('char2fam', 'family of char2',
                    'min_length[1]|max_length[6]|trim|numeric|xss_clean');
            $this->form_validation->set_rules('target_turn', 'target_turn',
                    'min_length[1]|max_length[6]|trim|numeric|xss_clean');
            
            return $this->form_validation->run();
            
        }
         
         
    }    /////////////// END: class Family extends CI_Controller 
    
}  //// END:  if (isset($_SESSION["user"]) && !empty($_SESSION["user"]))
else {
   // deny, log, redirect
    
    //$this->load->helper('url');
    //redirect('/dynasties2/start', 'location');
    
}


